#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class PermissionsListProvider : CY.GFive.Core.Providers.DALProvider.IPermissionsListProvider
	{
		#region Sql
		private static readonly string SqlSelectPermissionsList = "SELECT [Id], [ActionKey],[PModuleKey],[PermissionName] FROM [PermissionsList] WHERE  [Id]=@Id";

		private static readonly string SqlInsertPermissionsList = "INSERT INTO [PermissionsList]([ActionKey],[PModuleKey],[PermissionName]) values(@ActionKey,@PModuleKey,@PermissionName);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdatePermissionsList = "UPDATE [PermissionsList] SET [ActionKey] = @ActionKey,[PModuleKey] = @PModuleKey,[PermissionName] = @PermissionName WHERE  [Id]=@Id";

		private static readonly string SqlDeletePermissionsList = "DELETE [PermissionsList] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllPermissionsList = "SELECT [Id], [ActionKey],[PModuleKey],[PermissionName] FROM [PermissionsList]";
		#endregion

		#region IPermissionsListProvider Members
		public Core.Business.PermissionsList Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectPermissionsList);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.PermissionsList permissionsList = new Core.Business.PermissionsList();

				if (!reader.IsDBNull(0)) permissionsList.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) permissionsList.ActionKey = reader.GetInt32(1);
				if (!reader.IsDBNull(2)) permissionsList.PModuleKey = reader.GetInt32(2);
				if (!reader.IsDBNull(3)) permissionsList.PermissionName = reader.GetString(3);

				return permissionsList;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.PermissionsList permissionsList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ActionKey", SqlDbType.Int, permissionsList.ActionKey);
			sql.AddParameter("@PModuleKey", SqlDbType.Int, permissionsList.PModuleKey);
			sql.AddParameter("@PermissionName", SqlDbType.NVarChar, permissionsList.PermissionName);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertPermissionsList);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) permissionsList.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.PermissionsList permissionsList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ActionKey", SqlDbType.Int, permissionsList.ActionKey);
			sql.AddParameter("@PModuleKey", SqlDbType.Int, permissionsList.PModuleKey);
			sql.AddParameter("@PermissionName", SqlDbType.NVarChar, permissionsList.PermissionName);
			sql.AddParameter("@Id", SqlDbType.Int, permissionsList.Id);
			sql.ExecuteSql(SqlUpdatePermissionsList);
		}

		public void Delete(Core.Business.PermissionsList permissionsList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, permissionsList.Id);
			sql.ExecuteSql(SqlDeletePermissionsList);
		}

		public IList<Core.Business.PermissionsList> GetAllPermissionsList()
		{
			IList<Core.Business.PermissionsList> permissionsListlist = new List<Core.Business.PermissionsList>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllPermissionsList);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.PermissionsList permissionsList = new Core.Business.PermissionsList();

					if (!reader.IsDBNull(0)) permissionsList.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) permissionsList.ActionKey = reader.GetInt32(1);
					if (!reader.IsDBNull(2)) permissionsList.PModuleKey = reader.GetInt32(2);
					if (!reader.IsDBNull(3)) permissionsList.PermissionName = reader.GetString(3);

					permissionsList.MarkOld();
					permissionsListlist.Add(permissionsList);
				}
				reader.Close();
			}
			return permissionsListlist;
		}
		#endregion
	}
}
